#include <iostream>
#include "./inc/inner/linked_list_common.h"
#include "student_common.h"
#include "admin_common.h"
using namespace std;

// 函数声明
void add_admin(linked_list *list, admin *adm);
admin *find_admin(linked_list *list, int id);
bool update_admin(linked_list *list, int id, const string &name, const string &password);
bool delete_admin(linked_list *list, int id);
void print_admins(linked_list *list);

void add_student(linked_list *list, student *stu);
student *find_student(linked_list *list, int id);
bool update_student(linked_list *list, int id, const string &name, int age, const string &address);
bool delete_student(linked_list *list, int id);
void print_students(linked_list *list);

// 管理员菜单
void admin_menu(linked_list &adminList)
{
    while (true)
    {
        cout << "\n==== 管理员管理 ====" << endl;
        cout << "1. 添加管理员" << endl;
        cout << "2. 查找管理员" << endl;
        cout << "3. 修改管理员" << endl;
        cout << "4. 删除管理员" << endl;
        cout << "5. 显示所有管理员" << endl;
        cout << "0. 返回主菜单" << endl;
        cout << "请选择: ";
        int choice;
        cin >> choice;
        if (choice == 0)
            break;
        if (choice == 1)
        {
            int id;
            string username, password;
            cout << "输入ID: ";
            cin >> id;
            cout << "输入用户名: ";
            cin >> username;
            cout << "输入密码: ";
            cin >> password;
            add_admin(&adminList, new admin(id, username, password));
            cout << "添加成功!" << endl;
        }
        else if (choice == 2)
        {
            int id;
            cout << "输入要查找的ID: ";
            cin >> id;
            admin *a = find_admin(&adminList, id);
            if (a)
                a->show_admin_msg();
            else
                cout << "未找到该管理员!" << endl;
        }
        else if (choice == 3)
        {
            int id;
            string username, password;
            cout << "输入要修改的ID: ";
            cin >> id;
            cout << "新用户名: ";
            cin >> username;
            cout << "新密码: ";
            cin >> password;
            if (update_admin(&adminList, id, username, password))
                cout << "修改成功!" << endl;
            else
                cout << "未找到该管理员!" << endl;
        }
        else if (choice == 4)
        {
            int id;
            cout << "输入要删除的ID: ";
            cin >> id;
            if (delete_admin(&adminList, id))
                cout << "删除成功!" << endl;
            else
                cout << "未找到该管理员!" << endl;
        }
        else if (choice == 5)
        {
            print_admins(&adminList);
        }
        else
        {
            cout << "无效选择!" << endl;
        }
    }
}

// 学生菜单
void student_menu(linked_list &stuList)
{
    while (true)
    {
        cout << "\n==== 学生管理 ====" << endl;
        cout << "1. 添加学生" << endl;
        cout << "2. 查找学生" << endl;
        cout << "3. 修改学生" << endl;
        cout << "4. 删除学生" << endl;
        cout << "5. 显示所有学生" << endl;
        cout << "0. 返回主菜单" << endl;
        cout << "请选择: ";
        int choice;
        cin >> choice;
        if (choice == 0)
            break;
        if (choice == 1)
        {
            int id, age;
            string name, address;
            cout << "输入学号: ";
            cin >> id;
            cout << "输入年龄: ";
            cin >> age;
            cout << "输入姓名: ";
            cin >> name;
            cout << "输入地址: ";
            cin >> address;
            add_student(&stuList, new student(id, age, name, address));
            cout << "添加成功!" << endl;
        }
        else if (choice == 2)
        {
            int id;
            cout << "输入要查找的学号: ";
            cin >> id;
            student *stu = find_student(&stuList, id);
            if (stu)
                stu->show_student_msg();
            else
                cout << "未找到该学生!" << endl;
        }
        else if (choice == 3)
        {
            int id, age;
            string name, address;
            cout << "输入要修改的学号: ";
            cin >> id;
            cout << "新姓名: ";
            cin >> name;
            cout << "新年龄: ";
            cin >> age;
            cout << "新地址: ";
            cin >> address;
            if (update_student(&stuList, id, name, age, address))
                cout << "修改成功!" << endl;
            else
                cout << "未找到该学生!" << endl;
        }
        else if (choice == 4)
        {
            int id;
            cout << "输入要删除的学号: ";
            cin >> id;
            if (delete_student(&stuList, id))
                cout << "删除成功!" << endl;
            else
                cout << "未找到该学生!" << endl;
        }
        else if (choice == 5)
        {
            print_students(&stuList);
        }
        else
        {
            cout << "无效选择!" << endl;
        }
    }
}

// 主函数
int main()
{
    linked_list adminList, stuList;
    adminList.init();
    stuList.init();
    // 实验数据
    add_admin(&adminList, new admin(1, "admin1", "123456"));
    add_admin(&adminList, new admin(2, "admin2", "654321"));
    add_student(&stuList, new student(1001, 20, "张三", "北京"));
    add_student(&stuList, new student(1002, 21, "李四", "上海"));
    add_student(&stuList, new student(1003, 22, "王五", "广州"));
    while (true)
    {
        cout << "\n==== 主菜单 ====" << endl;
        cout << "1. 管理员管理" << endl;
        cout << "2. 学生管理" << endl;
        cout << "0. 退出" << endl;
        cout << "请选择: ";
        int choice;
        cin >> choice;
        if (choice == 0)
            break;
        if (choice == 1)
            admin_menu(adminList);
        else if (choice == 2)
            student_menu(stuList);
        else
            cout << "无效选择!" << endl;
    }
    cout << "程序已退出。" << endl;
    return 0;
}
